public class Test {
}

import java.util.ArrayList;
        import java.util.List;
        import java.util.Scanner;

public class StudentManager {
    private List<Student> students;
    private Scanner scanner;

    public StudentManager() {
        students = new ArrayList<>();
        scanner = new Scanner(System.in);
    }

    // 添加学生
    public void addStudent() {
        System.out.println("=== 添加学生 ===");
        System.out.print("输入学号: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        System.out.print("输入姓名: ");
        String name = scanner.nextLine();

        System.out.print("输入年龄: ");
        int age = scanner.nextInt();
        scanner.nextLine();

        System.out.print("输入专业: ");
        String major = scanner.nextLine();

        Student student = new Student(id, name, age, major);
        students.add(student);
        System.out.println("学生添加成功!");
    }

    // 查找学生
    public Student findStudent(int id) {
        for (Student student : students) {
            if (student.getId() == id) {
                return student;
            }
        }
        return null;
    }

    // 删除学生
    public boolean deleteStudent(int id) {
        Student student = findStudent(id);
        if (student != null) {
            students.remove(student);
            return true;
        }
        return false;
    }

    // 修改学生信息
    public boolean updateStudent(int id) {
        Student student = findStudent(id);
        if (student != null) {
            System.out.println("=== 修改学生信息 ===");
            System.out.println("当前信息: " + student);

            System.out.print("输入新姓名(不修改请直接回车): ");
            String name = scanner.nextLine();
            if (!name.isEmpty()) {
                student.setName(name);
            }

            System.out.print("输入新年龄(不修改请直接回车): ");
            String ageStr = scanner.nextLine();
            if (!ageStr.isEmpty()) {
                student.setAge(Integer.parseInt(ageStr));
            }

            System.out.print("输入新专业(不修改请直接回车): ");
            String major = scanner.nextLine();
            if (!major.isEmpty()) {
                student.setMajor(major);
            }

            return true;
        }
        return false;
    }

    // 显示所有学生
    public void displayAllStudents() {
        if (students.isEmpty()) {
            System.out.println("没有学生记录!");
            return;
        }

        System.out.println("=== 学生列表 ===");
        for (Student student : students) {
            System.out.println(student);
        }
    }

    // 主菜单
    public void showMenu() {
        int choice;
        do {
            System.out.println("\n=== 学生管理系统 ===");
            System.out.println("1. 添加学生");
            System.out.println("2. 查找学生");
            System.out.println("3. 删除学生");
            System.out.println("4. 修改学生信息");
            System.out.println("5. 显示所有学生");
            System.out.println("0. 退出系统");
            System.out.print("请输入你的选择: ");

            choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    System.out.print("输入要查找的学生学号: ");
                    int findId = scanner.nextInt();
                    Student foundStudent = findStudent(findId);
                    if (foundStudent != null) {
                        System.out.println("找到学生: " + foundStudent);
                    } else {
                        System.out.println("未找到该学生!");
                    }
                    break;
                case 3:
                    System.out.print("输入要删除的学生学号: ");
                    int deleteId = scanner.nextInt();
                    if (deleteStudent(deleteId)) {
                        System.out.println("学生删除成功!");
                    } else {
                        System.out.println("未找到该学生!");
                    }
                    break;
                case 4:
                    System.out.print("输入要修改的学生学号: ");
                    int updateId = scanner.nextInt();
                    if (updateStudent(updateId)) {
                        System.out.println("学生信息修改成功!");
                    } else {
                        System.out.println("未找到该学生!");
                    }
                    break;
                case 5:
                    displayAllStudents();
                    break;
                case 0:
                    System.out.println("感谢使用学生管理系统，再见!");
                    break;
                default:
                    System.out.println("无效的选择，请重新输入!");
            }
        } while (choice != 0);

        scanner.close();
    }

    public static void main(String[] args) {
        StudentManager manager = new StudentManager();
        manager.showMenu();
    }
}

